package Simulation;

import Config.Config;
import People.Animal;
import People.People;
import People.Person;
import People.Status;

/**
 * Models the simulation
 * 
 * @author Amir BEN SLIMANE
 */
public class Infection extends Manager {
	private Map map;

	/**
	 * Builder's Simulation
	 * 
	 * @param map
	 */
	public Infection(Map map) {
		this.setMap(map);
	}

	/**
	 * Seeks contagious close
	 */
	public void lookArround() {
		for (int x = 0; x < Config.MAP_HEIGTH; x++) {
			for (int y = 0; y < Config.MAP_LENGTH; y++) {
				if (this.map.getMapSimulation()[x][y].getStatus() == Status.Healthy) {
					look(x, y);
				}
			}
		}
	}

	private void look(int x, int y) {
		if (this.map.getMapSimulation()[Manager.mod(x + 1, Config.MAP_LENGTH)][y]
				.getStatus() == Status.Contagious
				|| this.map.getMapSimulation()[Manager.mod(x + 1,
						Config.MAP_LENGTH)][Manager.mod(y + 1,
						Config.MAP_HEIGTH)].getStatus() == Status.Contagious
				|| this.map.getMapSimulation()[x][Manager.mod(y + 1,
						Config.MAP_HEIGTH)].getStatus() == Status.Contagious
				|| this.map.getMapSimulation()[Manager.mod(x - 1,
						Config.MAP_LENGTH)][Manager.mod(y + 1,
						Config.MAP_HEIGTH)].getStatus() == Status.Contagious
				|| this.map.getMapSimulation()[Manager.mod(x - 1,
						Config.MAP_LENGTH)][y].getStatus() == Status.Contagious
				|| this.map.getMapSimulation()[Manager.mod(x - 1,
						Config.MAP_LENGTH)][Manager.mod(y - 1,
						Config.MAP_HEIGTH)].getStatus() == Status.Contagious
				|| this.map.getMapSimulation()[x][Manager.mod(y - 1,
						Config.MAP_HEIGTH)].getStatus() == Status.Contagious
				|| this.map.getMapSimulation()[Manager.mod(x + 1,
						Config.MAP_LENGTH)][Manager.mod(y - 1,
						Config.MAP_HEIGTH)].getStatus() == Status.Contagious) {
			if ((int) (Math.random() * 101) < this.map.getMapSimulation()[x][y]
					.getInfectionRate()) {
				this.map.getMapSimulation()[x][y].setStatus(Status.Sick);
			}
		}
	}

	/**
	 * @return the map
	 */
	public Map getMap() {
		return map;
	}

	/**
	 * @param map
	 *            the map to set
	 */
	public void setMap(Map map) {
		this.map = map;
	}

}
